package NewTmas.FramesandPanels;

import NewTmas.TableModels.JobTableModel;
import NewTmas.TableModels.TasksTableModel;
import NewTmas.TableModels.XTableColumnModel;
import com.ezware.oxbow.swingbits.table.filter.TableRowFilterSupport;
import taskmaster.auth.User;
import taskmaster.job.QueryParams;
import taskmaster.job.TaskBuilder;
import taskmaster.job.TaskCollection;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Date;
import java.util.Hashtable;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 7/6/11
 * Time: 2:57 PM
 * To create a Table panel for a job daemon states table or a task collection table.
 */
public class TablePanel
{


    JTable tcTable;
    TaskCollection tc;

    User user;
    Date date;
    QueryParams limiters;
    TaskBuilder tb;
    XTableColumnModel columnModel = new XTableColumnModel();

    TasksTableModel taskTableModel;
    JobTableModel jobTableModel;

    Hashtable allRolesHash;


    public TablePanel(TaskCollection taskCollection, TaskBuilder tb, QueryParams limiters, User user)
    {
        this.tc = taskCollection;
        this.tb = tb;
        this.limiters = limiters;
        this.user = user;
    }

    public TablePanel(User user, Date date, TaskBuilder tb, Hashtable allRolesHash)
    {
        this.date = date;
        this.user = user;
        this.tb = tb;
        this.allRolesHash = allRolesHash;
    }

    /*setting the table properties for Task collection table*/

    public JTable getTable()
    {

        taskTableModel = new TasksTableModel(tc);
        tcTable = new JTable(taskTableModel);
        tcTable.setAutoCreateRowSorter(true);
        tcTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        tcTable.setFillsViewportHeight(true);
        tcTable.setAutoCreateColumnsFromModel(true);


        TableRowFilterSupport.forTable(tcTable).searchable(true).apply();

        JTableHeader header = tcTable.getTableHeader();
        header.setBackground(Color.YELLOW);


        tcTable.setColumnModel(columnModel);
        tcTable.createDefaultColumnsFromModel();
        return tcTable;

    }

    public void showColumn(int columnIndex)
    {


        TableColumn column = columnModel.getColumnByModelIndex(columnIndex);
        boolean visible = columnModel.isColumnVisible(column);
        columnModel.setColumnVisible(column, !visible);

    }


    public JPanel getJobTablePanel()
    {

        JPanel panel = new JPanel();
        jobTableModel = new JobTableModel(user, date, allRolesHash);
        tcTable = new JTable(jobTableModel);

        tcTable.setColumnModel(columnModel);
        tcTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        TableRowFilterSupport.forTable(tcTable).apply();
        tcTable.setAutoCreateRowSorter(true);
        tcTable.setAutoCreateColumnsFromModel(true);
        tcTable.setFillsViewportHeight(true);
        tcTable.setAutoCreateRowSorter(true);
        tcTable.setVisible(true);
        tcTable.setSize(900, 900);
        tcTable.createDefaultColumnsFromModel();

        panel.setLayout(new BorderLayout());
        panel.add(new JScrollPane(tcTable), BorderLayout.CENTER);

        panel.setVisible(true);
        return panel;

    }

    public void refreshTaskTablePanel(TaskCollection tc)
    {
        taskTableModel.populateTable(tc);
        taskTableModel.fireTableDataChanged();
        // TasksTableModel taskTableModel = new TasksTableModel(tc);
//        ((AbstractTableModel)tcTable.getModel()).fireTableDataChanged();
//        tcTable.setModel(taskTableModel);
        tcTable.revalidate();
    }


    public void refreshJobStatesTablePanel()
    {
        jobTableModel.populateTable();
        jobTableModel.fireTableDataChanged();
//            tcTable.setModel(new JobTableModel(user,date));
        tcTable.revalidate();
    }

}
